layui.use(['laydate','form','table','laytpl','layer'], function(){
	var laydate = layui.laydate;
	var form = layui.form;
	var table = layui.table;
	var laytpl = layui.laytpl;
	var layer = layui.layer
	/*@时间*/
	laydate.render({
	  	elem: '#start' //指定元素
	});
	laydate.render({
	  	elem: '#end' //指定元素
	});

	/*@初始化table*/
	layui.tableObj = table.render({
	    elem: '#table-list',
	    id: 'list',
	    url: '/finance/refund/index', //数据接口
	    page: true, //开启分页
	    cols: [[ //表头
		    	{field: 'rbooking_id', title: '序号'},
		    	{field: 'addtime', title: '退款时间'},
		    	{field: 'supplier_name', title: '供应商'},
		    	{field: 'contract_sn', title: '合同编号'},
		    	{field: 'ty_text', title: '退款类型'},
		    	{field: 'goods_name', title: '产品名称'},
		    	{field: 'cat_f_name', title: '产品分类'},
		    	{field: 'cat_s_name', title: '产品子分类'},
		    	{field: 'brand_name', title: '品牌'},
		    	{field: 'marker', title: '型号'},
		    	{field: 'memory_name', title: '内存'},
		    	{field: 'attribute_name', title: '属性'},
		    	{field: 'colour_name', title: '颜色'},
		    	{field: 'number', title: '退货数量'},
		    	{field: 'unit', title: '单位'},
		    	{field: 'buying_price', title: '进货单价（元）'},
		    	{field: 'money', title: '退款总额（元）'},
		    	{title: '操作', toolbar: '#table-btn'}
		    ]],
	    parseData: function(res){
	    	let coun = res.data.count > 0 ? res.data.count : '0'
	    	res.data = res.data.list
	    	let getTpl_2 = oper_box.innerHTML;
			let count = document.getElementById('oper-box');
			laytpl(getTpl_2).render(coun, function(html){
				count.innerHTML = html;
			});
	    }
	});

	// /*@查询*/
	form.on('submit(search)',function(data){
		if(data.field.start != '' && data.field.end != ''){
			var startDate = new Date(data.field.start).getTime();
			var endDate = new Date(data.field.end).getTime();
			if(startDate > endDate){
				layer.msg('开始时间不能大于结束时间',{icon: 2})
				return false
			}
		}
		/*重载表格数据*/
		layui.tableObj.reload({
			page: {
                page: 1 //重新从第 1 页开始
            },
            where: data.field
        });
        return false
	})
	/*@监听工具*/
	table.on('tool(list)', function(obj_tr){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
	  	var data = obj_tr.data; //获得当前行数据
	  	var layEvent = obj_tr.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
	  	var tr = obj_tr.tr; //获得当前行 tr 的DOM对象
	  	if(layEvent === 'del'){ //删除
	  		let obj_data = {}
	  		obj_data.id = data.rbooking_id
	  		del(obj_data)
	  	}
	});
	/*@删除*/
	function del(obj_data){
		$.post('/finance/refund/del', obj_data, function(data){
			if(data.code !== 1){
				layer.msg(data.msg, {icon: 5});
			}else{
				layer.msg(data.msg, {icon: 6, time: 1000}, function(){
					table.reload('list')
				})
			}
		})
	}
});